﻿using System;
using OpenWaves.Web;

[assembly: PublicAssemblyResources("OpenWaves.EPiServer.AdminTools.LogViewer.UI.FileContentPage.aspx")]

namespace OpenWaves.EPiServer.AdminTools.LogViewer.UI
{
    public partial class FileContentPage : AdminPageBase
    {
        private readonly LogFilePresenter presenter = new LogFilePresenter();

        protected LogFile File
        {
            get { return this.presenter.File; }
        }

        protected string ErrorMessage
        {
            get { return this.presenter.ErrorMessage; }
        }

        protected string WarningMessage
        {
            get { return this.presenter.WarningMessage; }
        }

        protected Exception Exception
        {
            get { return this.presenter.Exception; }
        }

        protected string FileContent { get; private set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        protected Url GetDownloadUrl()
        {
            if (this.presenter.File == null)
            {
                return null;
            }

            UrlBuilder builder = AssemblyResourceVirtualPathProvider.CreateUrl(typeof(LogFileDownload)).GetBuilder();
            builder.Query.Add(LogFileListing.PathQueryStringParameter, this.presenter.File.Path);
            return builder.ToUrl();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        protected  Url GetViewUrl()
        {
            if (this.presenter.File == null)
            {
                return null;
            }

            UrlBuilder builder = AssemblyResourceVirtualPathProvider.CreateUrl(typeof(FileContentPage)).GetBuilder();
            builder.Query.Add(LogFileListing.PathQueryStringParameter, this.presenter.File.Path);
            return builder.ToUrl();
        }


        protected override void OnLoad(EventArgs e)
        {
            this.presenter.Path = this.Request.QueryString[LogFileListing.PathQueryStringParameter];
            this.presenter.DisplayedContentLength = BinaryUnit.FromKilobytes(200);
            this.FileContent = this.presenter.GetContent();
        }


        protected override void OnLoadComplete(EventArgs e)
        {
            this.DataBind();
        }

        protected static string TranslateFallback(string key, string fallback)
        {
            return LogViewerPlugin.TranslateConfigurationFallback(key, fallback);
        }
    }
}